package com.fuqi.android.zhongcai.base.view.activity;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.view.widget.TitleBar;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

public abstract class BaseActivity extends FragmentActivity {

    public TitleBar titleBar;
    protected int currentPage = 1;
    protected int showCount = 10;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        if (hasTitleBar()) {
            initTitleBar();
        }

        initView();
        initData();
        initListener();
    }


    private void initTitleBar() {

        titleBar = (TitleBar) findViewById(R.id.title_bar);

        if (lowKitKat()) {
            titleBar.setImmersive(false);
        } else if (hasKitKat() && !hasLollipop()) {
            //透明状态栏
            titleBar.setImmersive(true);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        } else if (hasLollipop()) {
            Window window = getWindow();
            titleBar.setImmersive(true);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#00ffffff"));
        }

        titleBar.setLeftImageResource(R.mipmap.title_back);
        titleBar.setLeftTextColor(Color.WHITE);
        titleBar.setTitleColor(Color.WHITE);
        titleBar.setTitleSize((float)18.5);
        titleBar.setSubTitleColor(Color.WHITE);
        titleBar.setActionTextColor(Color.WHITE);
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBar.setBackgroundColor(Color.parseColor("#4DB852"));

    }


    public boolean lowKitKat() {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
    }

    public boolean hasKitKat() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    public boolean hasLollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public abstract void initView();
    public abstract void initListener();
    public abstract void initData();
    /**
     *  hasTitleBar()
     * 返回true， 每个Activity的布局文件 都要添加(id不能变)
     * <com.android.quick.widget.TitleBar
     * android:id="@+id/title_bar"
     * android:layout_width="match_parent"
     * android:layout_height="@dimen/title_height"
     * android:layout_alignParentTop="true" />
     *
     * @return
     */

    public abstract boolean hasTitleBar();
    public abstract int getLayoutId();
}
